#include<stdio.h>
#include<stdlib.h>
struct game
{
	int fine,time;
};
int compare(const void* a, const void* b) 
{
    return ((game*)b)->fine - ((game*)a)->fine;
}
int main()
{
	int m,n;
	scanf("%d %d",&m,&n);
	struct game* a = (struct game*)malloc(n * sizeof(struct game));
	int maxdeadline=0;
	for(int i=0;i<n;i++)
	{
		scanf("%d",&a[i].time);
		if(a[i].time>maxdeadline)
		maxdeadline=a[i].time;
	}
	for(int i=0;i<n;i++)
	scanf("%d",&a[i].fine);
	qsort(a,n,sizeof(struct game),compare);
	 int* t = (int*)calloc(maxdeadline + 1, sizeof(int));
	for(int i=0;i<n;i++)
	{
		int j;
		for(j=a[i].time;j>0;j--)
		{
			if(t[j]==0)
			{
				t[j]=1;
				break;
			}
		}
		if (j < 0) 
		{
            m -= a[i].fine;
        }
	} 
	printf("%d", m);
	free(a);
    free(t);
	return 0;
}
/*正确
#include <stdio.h>
#include <stdlib.h>

typedef struct {
    int deadline;
    int penalty;
} Game;

int compare(const void *a, const void *b) {
    Game *gameA = (Game *)a;
    Game *gameB = (Game *)b;
    if (gameB->penalty != gameA->penalty) {
        return gameB->penalty - gameA->penalty;
    } else {
        return gameA->deadline - gameB->deadline;
    }
}

int maxRemainingMoney(int m, int n, int deadlines[], int penalties[]) {
    Game games[n];
    for (int i = 0; i < n; i++) {
        games[i].deadline = deadlines[i];
        games[i].penalty = penalties[i];
    }
    
    qsort(games, n, sizeof(Game), compare);
    
    int maxDeadline = 0;
    for (int i = 0; i < n; i++) {
        if (games[i].deadline > maxDeadline) {
            maxDeadline = games[i].deadline;
        }
    }
    
    int *timeSlots = (int *)calloc(maxDeadline + 1, sizeof(int));
    int totalPenalty = 0;
    
    for (int i = 0; i < n; i++) {
        int deadline = games[i].deadline;
        int found = 0;
        for (int j = deadline; j > 0; j--) {
            if (timeSlots[j] == 0) {
                timeSlots[j] = 1;
                found = 1;
                break;
            }
        }
        if (!found) {
            totalPenalty += games[i].penalty;
        }
    }
    
    free(timeSlots);
    return m - totalPenalty;
}

int main() {
    int m, n;
    scanf("%d", &m);
    scanf("%d", &n);
    
    int deadlines[n];
    for (int i = 0; i < n; i++) {
        scanf("%d", &deadlines[i]);
    }
    
    int penalties[n];
    for (int i = 0; i < n; i++) {
        scanf("%d", &penalties[i]);
    }
    
    int result = maxRemainingMoney(m, n, deadlines, penalties);
    printf("%d\n", result);
    
    return 0;
}
*/